package com.wxtf.platform.tools;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author 黄威
 * */
public class ConstantInfoHelper {
	
	public static final String KEY_MESSAGE_RESULT = "result";
	public static final String KEY_MESSAGE_RESULTMESSAGE = "resultmessage";
	public static final String KEY_MESSAGE_RESULT_RECORD = "resultrecord";
	public static final String[] JSON_HEAD = new String[] { KEY_MESSAGE_RESULT, KEY_MESSAGE_RESULTMESSAGE, KEY_MESSAGE_RESULT_RECORD};
	
	
	public static final String KEY_CLASSNAME = "classname";
	public static final String KEY_METHODNAME = "methodname";
	public static final String KEY_LOGICDATA = "logicdata";
	public static final String KEY_EXTENDDATA = "extenddata";
	
	
	public static final String VALUE_MESSAGE_RESULT_TRUE = "true";
	public static final String VALUE_MESSAGE_RESULT_FALSE = "false";
	//返回失败信息
    public static final String VALUE_MESSAGE_CALL_FAIL = "调用失败";
    //返回成功信息
    public static final String VALUE_MESSAGE_CALL_SUCCESS = "调用成功"; 
    //返回的结果集
    public static final String VALUE_MESSAGE_CALL_RECORD = "none"; 
    //应用程序路径
    public static String APPLICATION_PATH = "";
    public static HiberNateBaseDao hnbd = null;
 
    public static HiberNateBaseDao getBaseDao(String confFileName,String daoID){
    	if (hnbd==null){
    		AbstractApplicationContext ctx= new ClassPathXmlApplicationContext(confFileName);//"springhibernate.xml");
		    hnbd = (HiberNateBaseDao)ctx.getBean(daoID);//"commonsHiberNateBaseDao");
    	}
    	return hnbd;
    	
    }
    
    public static String generateCurrentDateTime(){
    	String sRe = null;
		Date d = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		sRe = sdf.format(d);
		return sRe;
    }
    /**
     * 根据指定的字符串格式来返回时间日期
     * yyyyMMddHHmmssSSS
     * */
    public static String generateCurrentDateTime(String symbol){
    	String sRe = null;
		Date d = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat(symbol);
		sRe = sdf.format(d);
		return sRe;
    }
    
    /** 
     * 描述：generateJSONString 
     * @param json 传入的是json串
     * @return
     * @CreateOn Jul 10, 2012  10:53:58 AM
     * @author chun_chang
     */
    public static String generateJSONString(String json){
		return "{\"result\":true,\"resultmessage\":\"调用成功\",\"resultrecord\":" + json + "}";
    }
    
    public static String generateErrorJSONString(){
    	return "{\"result\":false,\"resultmessage\":\"调用失败\",\"resultrecord\":\"操作失败\"}";
    }
    
    /**
     * @param keys  
     * @param values
     * @param isSingleQuotation true是单引号，false是双引号
     * @return
     */
    public static String generateJSONString(String[]keys,String[]values,boolean isSingleQuotation ){
    	String sRe = "";
    	try{
    		for(int i1=0;i1<keys.length;i1++){
    			if(sRe.length()==0){
    				if(isSingleQuotation){
    					sRe = "'" + keys[i1] + "':" + "'" +  values[i1] +  "'";
    				}else{
    					sRe = "\"" + keys[i1] + "\":" + "\"" +  values[i1] +  "\"";
    				}
    			}else{
    				if(isSingleQuotation){
    					sRe = sRe + "," + "'" + keys[i1] + "':" + "'" +  values[i1] +  "'";
    				}else{
    					sRe = sRe + "," + "\"" + keys[i1] + "\":" + "\"" +  values[i1] +  "\"";
    				}
    				
    			}
    		}
    		if(sRe.length()!=0){
    			//sRe = "{" + sRe + "}";
    				sRe = "{" + sRe + "}" ;
    		}
    	}catch(Exception ex){
    		ex.printStackTrace();
    	}
    	return sRe;
    }
    
    /**
     * @param keyValue
     * @param isSingleQuotation true是单引号，false是双引号
     * @return
     */
    @SuppressWarnings("unchecked")
	public static String generateJSONString(Map[]keyValue, boolean isSingleQuotation ){
    	String rows = "";
    	try{
    		String row = "";
    		Object []keys = null;

    		for(int i2=0; i2<keyValue.length ;i2++){
    			keys = new Object[keyValue[i2].keySet().toArray().length];
    			keys = keyValue[i2].keySet().toArray();
    			
    			
    			for(int i1=0;i1<keys.length;i1++){
        			if(row.length()==0){
        				if(isSingleQuotation){
        					row = "'" + keys[i1] + "':" + "'" + keyValue[i2].get(keys[i1]) +  "'";
        				}else{
        					row = "\"" + keys[i1] + "\":" + "\"" + keyValue[i2].get(keys[i1]) +  "\"";
        				}
        			}else{
        				if(isSingleQuotation){
        					row = row + "," + "'" + keys[i1] + "':" + "'" +  keyValue[i2].get(keys[i1]) +  "'";
        				}else{
        					row = row + "," + "\"" + keys[i1] + "\":" + "\"" +  keyValue[i2].get(keys[i1]) +  "\"";
        				}
        				
        			}
        		}
    			if(rows.length() ==0){
    				rows = "{" + row + "}" ;
    				row = "";
    			}else {
    				rows = rows +  ",{" + row + "}" ;
    			} 
    		}
    		
    		
    		if(rows.length()!=0){
    				rows = "[" + rows + "]";
    		}
    	}catch(Exception ex){
    		ex.printStackTrace();
    	}
    	return rows;
    }
    
	public static String[] getSuccResultDetail(String json) {
		return new String[] {
			ConstantInfoHelper.VALUE_MESSAGE_RESULT_TRUE,
			ConstantInfoHelper.VALUE_MESSAGE_CALL_SUCCESS,
			json
		};
	}
	
	public static String[] getFailResultDetail(String json) {
		return new String[] {
				ConstantInfoHelper.VALUE_MESSAGE_RESULT_FALSE,
				ConstantInfoHelper.VALUE_MESSAGE_CALL_FAIL,
				json
		};
	}
    
    
}
